<template>
  <div class="app-container">
    <div>
      <br/>
      <span>对订单有疑问?拨打0311-46398627</span><br/>
      <span>流程：支付完成-》等待服务-》确认完成服务-》评价</span><br/>
      <span>可申请退款时间最后期限为服务时间的前一天</span><br/>
    </div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编码" prop="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="请输入客户编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          :disabled = "customerVisible"
        />
      </el-form-item>
      <el-form-item label="律师编码" >
        <el-select prop="lawyerCode" v-model="queryParams.lawyerCode" clearable size="small">
        <el-option 
        v-for="dict in lawyerOptions"
                  :key="dict.lawyerCode"
                  :label="dict.lawyerName"
                  :value="dict.lawyerCode"
                  :disabled = "lawyerVisible"
        />
        </el-select>
      </el-form-item>
      <el-form-item label="订单创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:orderDetail:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:orderDetail:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleGreen"
          v-hasPermi="['business:orderDetail:green']"
        >同意退款</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:orderDetail:export']"
        >在线订单导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderDetailList" @selection-change="handleSelectionChange" stripe="true" >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="订单号" align="center" prop="orderId"  width="180"/>
      <el-table-column label="客户编码" align="center" prop="customerCode" />
      <el-table-column label="律师名称" align="center" prop="lawyerName" />
      <el-table-column label="服务日期" align="center" prop="serviceDate" width="120"/>
      <el-table-column label="服务时间" align="center" prop="serviceTime" width="120"/>
      <el-table-column label="昵称" align="center" prop="name" width="100"/>
      <el-table-column label="联系方式" align="center" prop="customerTel" width="120"/>
      <el-table-column label="订单创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="订单状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="综合评分" align="center" prop="score" />
      <el-table-column label="评价" align="center" prop="appraise" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            :disabled="Date.parse(scope.row.serviceDate)<=Date.now()?true:false"
            v-if="scope.row.status == 1"
            v-hasPermi="['business:orderDetail:edit']"
          >修改电话
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-undo"
            v-hasPermi="['business:orderDetail:cancel']"
            @click="handleCancel(scope.row)"
            :disabled="Date.parse(scope.row.serviceDate)<=Date.now()+1000*60*60*24?true:false"
            v-if="scope.row.status == 1"
          >申请退款</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-ok"
            @click="handleOk(scope.row)"
            v-hasPermi="['business:orderDetail:ok']"
            :disabled="scope.row.status != 1"
            v-if="Date.parse(scope.row.serviceDate) <= Date.now()"
          >完成服务</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-star-on"
            @click="handleScore(scope.row)"
            v-hasPermi="['business:orderDetail:score']"
            :disabled="scope.row.score != null?true:false"
            v-if="scope.row.status == 2"
          > 评价
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-star-on"
            @click="handleGreen(scope.row)"
            v-hasPermi="['business:orderDetail:green']"
            :disabled="scope.row.score == 4?true:false"
            v-if="scope.row.status == 3"
          > 同意退款
          </el-button> 
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div>
      <score :visible.sync = "scoreVisible" ref="score" @changeShow="getList" append-to-body>
      </score>
    </div>

    <!-- 添加或修改订单详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="联系方式" prop="customerTel">
          <el-input v-model="form.customerTel" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrderDetail, getOrderDetail, okOrderDetail, addOrderDetail, updateOrderDetail, exportOrderDetail ,cancelOrderDetail ,greenOrderDetail} from "@/api/business/orderDetail";
import { getRole } from "@/api/business/book";
import { listCustomerLawyer } from "@/api/business/customerlawyer";
import Score from './score.vue';
export default {
  name: "OrderDetail",
  components: {
    Score
  },
  data() {
    return {
      customerVisible:false,
      lawyerVisible:false,
      scoreVisible:false,
      lawyerOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单详情表格数据
      orderDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 订单创建时间时间范围
      daterangeCreateTime: [],
      // 有效期限时间范围
      daterangeDeadline: [],
      // 订单状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        customerCode: undefined,
        lawyerCode: undefined,
        createTime: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerTel: [
          { required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDept();
    this.getlawyer();
    this.getDicts("poppy_order_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    //获取律师列表
    getlawyer(){
      console.log("wewer")
      listCustomerLawyer().then(response=>{
        console.log(response)
        this.lawyerOptions = response.rows;
      })
    },
    //评价按钮
    handleScore(row){
      this.scoreVisible = true;
      this.$nextTick(() => {
        this.$refs.score.init(row.id);
      });
    },
    //获取当前登陆用户所属部门及角色
    getDept(){
      getRole().then(response=>{
        console.log("当前登录用户信息")
        console.log(response)
        if(response.deptId == 205){
          this.queryParams.customerCode = response.userName;
          this.customerVisible = true;
        }
        if(response.deptId == 108){
          this.queryParams.lawyerCode = response.userName;
          this.lawyerVisible = true;
        }
        this.getList();
      })
    },
    /** 查询订单详情列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeDeadline && '' != this.daterangeDeadline) {
        this.queryParams.params["beginDeadline"] = this.daterangeDeadline[0];
        this.queryParams.params["endDeadline"] = this.daterangeDeadline[1];
      }
      console.log(this.queryParams)
      listOrderDetail(this.queryParams).then(response => {
        this.orderDetailList = response.rows;
        console.log(response.rows)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 订单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        // customerCode: null,
        // lawyerCode: null,
        createTime: null,
        deadline: null,
        amount: null,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeDeadline = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrderDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改联系方式";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log("修改手机号")
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrderDetail(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrderDetail(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 确认完成服务按钮操作 */
    handleOk(row) {
      this.$confirm('是否确认完成订单编号为"' + row.orderId + '"的服务?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return okOrderDetail(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("已确认完成服务");
        })
    },
    //申请退款按钮操作
    handleCancel(row){
      this.$confirm('是否确认申请退款?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return cancelOrderDetail(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("已提交退款申请");
        })
    },
    //同意退款申请按钮
    handleGreen(row){
      // const ids = row.id || this.ids;
      this.$confirm('是否确认同意订单编号为"' + row.orderId + '"的退款申请?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return greenOrderDetail(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("已同意退款申请");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单详情数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrderDetail(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
